/*
 * Copyright (c)e 2024-2024.Powered by neflibata
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.liuzian.marketserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzian.marketserver.dto.R;
import com.liuzian.marketserver.entity.Message;
import com.liuzian.marketserver.entity.User;
import com.liuzian.marketserver.entity.Commodity;
import com.liuzian.marketserver.entity.Store;
import com.liuzian.marketserver.mapper.StoreMapper;
import com.liuzian.marketserver.service.CommodityService;
import com.liuzian.marketserver.service.ImageService;
import com.liuzian.marketserver.service.StoreService;
import com.liuzian.marketserver.service.UserService;
import com.liuzian.marketserver.service.MessageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

    /**
     * commodityService
     */
    @Lazy
    @Resource
    private CommodityService commodityService;

    /**
     * userService
     */
    @Lazy
    @Resource
    private UserService userService;

    /**
     * imageServiceImpl
     */
    @Lazy
    @Resource
    private ImageService imageServiceImpl;

    /**
     * messageServiceImpl
     */
    @Lazy
    @Resource
    private MessageService messageServiceImpl;

    /**
     * @param sid sid
     * @author liuzian
     */
    @Override
    public Store getStoreBySid(final Long sid) {
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Store::getSid, sid);
        queryWrapper.eq(Store::getIsAuth, 1);
        return super.getOne(queryWrapper);
    }

    /**
     * @param uid uid
     * @author liuzian
     */
    @Override
    public Store getStoreByUid(final Long uid) {
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Store::getUid, uid);
        queryWrapper.eq(Store::getIsAuth, 1);
        return super.getOne(queryWrapper);
    }

    /**
     * @param store store
     * @author liuzian
     */
    @Override
    public R<String> applyStore(final Store store) {
        log.info("apply store {}", store);
        store.setCreateTime(new Date());
        boolean bool = super.save(store);
        if (bool) {
            Message message = new Message();
            message.setCreateTime(new Date());
            message.setTitle("店铺申请通知");
            message.setContent("用户" + store.getUid() + "发来了一条店铺申请");
            message.setUid(Long.valueOf("1420527616925929856"));
            messageServiceImpl.publishMessage(message);
            return R.success("申请已提交");
        } else {
            return R.error("提交失败");
        }
    }

    /**
     * @param sid sid
     * @author liuzian
     */
    @Override
    public R<Store> queryStoreBySid(final Long sid) {
        log.info("query store by sid {}", sid);
        Store store = getStoreBySid(sid);
        LambdaQueryWrapper<Commodity> commodityQueryWrapper = new LambdaQueryWrapper<>();
        commodityQueryWrapper.eq(Commodity::getSid, sid);
        List<Commodity> commodities = commodityService.list(commodityQueryWrapper);
        store.setCommodities(commodities);
        return R.success(store);
    }

    /**
     * @param uid uid
     * @author liuzian
     */
    @Override
    public R<List<Store>> queryStoreByUid(final Long uid) {
        log.info("query store by uid {}", uid);
        LambdaQueryWrapper<Store> storeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        storeLambdaQueryWrapper.eq(Store::getUid, uid);
        storeLambdaQueryWrapper.eq(Store::getIsAuth, 1);
        List<Store> stores = super.list(storeLambdaQueryWrapper);
        LambdaQueryWrapper<Commodity> commodityQueryWrapper;
        for (Store store : stores) {
            commodityQueryWrapper = new LambdaQueryWrapper<>();
            commodityQueryWrapper.eq(Commodity::getSid, store.getSid());
            commodityQueryWrapper.eq(Commodity::getIsAuth, 1);
            List<Commodity> commodities = commodityService.list(commodityQueryWrapper);
            for (Commodity commodity : commodities) {
                commodity.setImage(imageServiceImpl.getImageByGid(commodity.getGid()));
            }
            store.setCommodities(commodities);
        }
        return R.success(stores);
    }

    /**
     * @author liuzian
     */
    @Override
    public R<List<Store>> verifyStore() {
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Store::getIsAuth, 0);
        List<Store> stores = super.list(queryWrapper);
        LambdaQueryWrapper<User> userLambdaQueryWrapper;
        for (Store store : stores) {
            userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getUid, store.getUid());
            store.setUser(userService.getOne(userLambdaQueryWrapper));
        }
        return R.success(stores);
    }

    /**
     * @param sid sid
     * @author liuzian
     */
    @Override
    public R<String> authStore(final Long sid) {
        LambdaUpdateWrapper<Store> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Store::getSid, sid);
        updateWrapper.set(Store::getIsAuth, 1);
        updateWrapper.set(Store::getAuthTime, new Date());
        updateWrapper.set(Store::getStatus, 1);
        boolean bool = super.update(updateWrapper);
        if (bool) {
            LambdaQueryWrapper<Store> storeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            storeLambdaQueryWrapper.eq(Store::getSid, sid);
            Store store = super.getOne(storeLambdaQueryWrapper);
            Message message = new Message();
            message.setCreateTime(new Date());
            message.setTitle("店铺审核结果通知");
            message.setContent("恭喜您，您的店铺申请审核通过啦。");
            message.setUid(store.getUid());
            messageServiceImpl.publishMessage(message);
            return R.success("审核通过");
        } else {
            return R.error("修改失败");
        }
    }

    /**
     * @param sid sid
     * @author liuzian
     */
    @Override
    public R<String> refuseStore(final Long sid) {
        LambdaUpdateWrapper<Store> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Store::getSid, sid);
        updateWrapper.set(Store::getIsAuth, 2);
        boolean bool = super.update(updateWrapper);
        if (bool) {
            Store store = super.getOne(updateWrapper);
            Message message = new Message();
            message.setCreateTime(new Date());
            message.setTitle("店铺审核结果通知");
            message.setContent("很遗憾，您的店铺申请审核不予通过。");
            message.setUid(store.getUid());
            messageServiceImpl.publishMessage(message);
            return R.success("审核不予通过");
        } else {
            return R.error("修改失败");
        }
    }

    @Override
    public R<Boolean> updateStoreCount(Long sid) {
        LambdaUpdateWrapper<Store> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Store::getSid, sid);
        updateWrapper.setSql("count = count + 1");
        boolean bool = super.update(updateWrapper);
        return R.success(bool);
    }
}
